package com.srpingde.hhh;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.springde.hhh.R;
import com.srpingde.hhh.application.HhhApp;
import com.srpingde.hhh.application.HHHDataManage;
import com.srpingde.hhh.bean.FenLei;
import com.srpingde.hhh.bean.FenLeiDetailBean;
import com.srpingde.hhh.bean.FenLeiDetailDataBean;
import com.srpingde.hhh.http.AsyncHttpResponseHandler;
import com.srpingde.hhh.jiekou.HHHIml;
import com.srpingde.hhh.jiekou.HHHInterface;

public class FenLeiActivity extends Activity {
	RadioGroup group;
	RadioButton radio;
	private List<RadioButton> radbtn=new ArrayList<RadioButton>();
	GridView gridView;
	HHHIml weiQuIml;
	EditText fenlei_edit;
	FenLei fenLei;
	Bitmap bp;
	boolean ischeck;
	String kind = "6";
	FenLeiDetailBean fenLeiDetailBean;
	//ImageView imageView1;
	List<Map<String, String>> fenleilist;
	private String tag = "FenLeiActivity";
    private List<Integer>rb=new ArrayList<Integer>();
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fen_lei);
		// 初始化
		init();
		// 读取数据
		getDaTa("&pid=6", "ishot", false);

	}

	// 初始化
	private void init() {
		group = (RadioGroup) findViewById(R.id.radiogroup);
		gridView = (GridView) findViewById(R.id.fenlei_lv);
		fenlei_edit = (EditText) findViewById(R.id.fenlei_edit);
		weiQuIml = new HHHIml();
		String str = HHHInterface.httpHeader + "api.php?tab=catalog&act=data&pid=0";
		weiQuIml.fenLei(FenLeiActivity.this, str,
				new AsyncHttpResponseHandler() {
					@Override
					public void onSuccess(String content) {
						super.onSuccess(content);
						// Log.e(tag,content+"总的分类数据~~~~~~~~~~");
						try {
							JSONObject jsonObject = new JSONObject(content);
							JSONArray array = jsonObject.getJSONArray("rows");
							fenleilist = new ArrayList<Map<String, String>>();
							for (int i = 0; i < array.length(); i++) {
								JSONObject object = array.getJSONObject(i);
								Map<String, String> map = new HashMap<String, String>();
								map.put("cid", object.getString("cid"));
								map.put("name", object.getString("name"));
								map.put("keyword", object.getString("keyword"));
								map.put("url", object.getString("url"));
								fenleilist.add(map);
								radio = (RadioButton) getLayoutInflater().inflate(R.layout.radiobutton, null);
								radio.setId(i);
								radio.setTag(object.getString("cid"));
								radio.setText(object.getString("name"));
								radbtn.add(radio);
								group.addView(radio);
								rb.add(radio.getId());
							}
							group.check(rb.get(0));
							for (int i = 0; i < fenleilist.size(); i++) {
								Log.i(tag, fenleilist.get(i).get("name")
										.toString());
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					}
				});

		fenlei_edit.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				if (keyCode == KeyEvent.KEYCODE_ENTER
						&& event.getAction() == KeyEvent.ACTION_DOWN) {
					search(null);
					return true;
				}
				return false;
			}
		});
		group.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				
				for(int i=0;i<rb.size();i++)
				{
					if(rb.get(i)==checkedId)
					{
						getDaTa(radbtn.get(i).getTag().toString(), "ishot", false);
						kind=radbtn.get(i).getTag().toString();
						break;
					}
				}
			}
		});
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent in = new Intent(FenLeiActivity.this,
						XiangQingActivity.class);
				in.putExtra("id", fenLeiDetailBean.getRows().get(arg2).getPid());
				startActivity(in);
			}
		});
	}

	public void search(View v) {
		if (fenlei_edit.getText().toString().trim().equals("")) {
			HhhApp.getInstance().showDialog(FenLeiActivity.this, "搜索内容不能为空！");
		} else {
			Intent in = new Intent(FenLeiActivity.this, SearchActivity.class);
			in.putExtra("search", fenlei_edit.getText().toString().trim());
			startActivity(in);
		}
	}

	// 读取数据
	private void getDaTa(String cid, String content, boolean check) {
		String str = HHHInterface.httpHeader + "api.php?tab=seachproducts&act=data&pagesize=100&page=1&cid=";
		if (check) {
			if (ischeck) {
				str = str + cid + "&sort=d";
			} else {
				str = str + cid + "&sort=a";
			}
		} else {
			str = str + cid + "&condition=" + content;
		}
		Log.e("mytag", str);
		weiQuIml.fenLeiList(str, FenLeiActivity.this,
				new AsyncHttpResponseHandler() {
					@Override
					public void onSuccess(String content) {
						super.onSuccess(content);
						Log.e("mytag", content + "=======分类的数据=");
						fenLeiDetailBean = HHHDataManage.getInstance()
								.getFenLeiDetailBean();
						gridView.setAdapter(new FenLeiListViewAdapter(
								FenLeiActivity.this, fenLeiDetailBean.getRows()));
					}
				});
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.remen:
			getDaTa(kind, "ishot", false);
			break;
		case R.id.xinpin:
			getDaTa(kind, "isnew", false);
			break;
		case R.id.gaodaodi:
			if (ischeck) {
				ischeck = false;
			} else {
				ischeck = true;
			}
			getDaTa(kind, "ishot", true);
			break;
		}
	}

	class FenLeiListViewAdapter extends BaseAdapter {
		List<FenLeiDetailDataBean> listBeans;
		Context context;

		public FenLeiListViewAdapter(Context context,
				List<FenLeiDetailDataBean> listBeans) {
			super();
			this.context = context;
			this.listBeans = listBeans;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return listBeans.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			// TODO Auto-generated method stub
			fenLeiHolder holder = null;
			if (arg1 == null) {
				holder = new fenLeiHolder();
				arg1 = LayoutInflater.from(context).inflate(
						R.layout.fenlei_item_adapter, null);
				holder.icon = (ImageView) arg1
						.findViewById(R.id.fenlei_item_icon);
				holder.item_price = (TextView) arg1
						.findViewById(R.id.item_price);
				holder.item_name = (TextView) arg1
						.findViewById(R.id.item_name);
				arg1.setTag(holder);
			} else {
				holder = (fenLeiHolder) arg1.getTag();
			}
			itemLayout(holder.icon);
			// holder.icon.setBackgroundResource(images[arg0]);
			holder.item_name.setText(listBeans.get(arg0).getName());
			holder.item_price.setText("  ￥"
					+ listBeans.get(arg0).getPrice());
			final ImageView kvShow = holder.icon;
			ImageLoader.getInstance().loadImage(
					HHHIml.httpHeader + listBeans.get(arg0).getUrl(),
					new SimpleImageLoadingListener() {
						@Override
						public void onLoadingComplete(String imageUri,
								View view, Bitmap bitmap) {
							BitmapDrawable bitmapDrawable = new BitmapDrawable(
									bitmap);
							// kvShow.setImageBitmap(bitmap);
							kvShow.setBackgroundDrawable(bitmapDrawable);
						}
					});
			return arg1;
		}

		class fenLeiHolder {
			ImageView icon;
			TextView item_price,item_name;
		}
	}

	private void itemLayout(ImageView imageView) {
		int wh = (int) (HhhApp.getInstance().getDisplayHightAndWightPx()[1] * 0.43);
		RelativeLayout.LayoutParams layoutParams = (LayoutParams) imageView
				.getLayoutParams();
		layoutParams.height = (int) (HhhApp.getInstance()
				.getDisplayHightAndWightPx()[0]
				- HhhApp.getInstance().title_h()
				* 0.7
				- HhhApp.getInstance().title_h()
				- HhhApp.getInstance().title_h()
				* 0.7
				- HhhApp.getInstance().title_h()
				* 0.7
				- HhhApp.getInstance().getDisplayHightAndWightPx()[0] * 0.065 - 20) / 3;
		layoutParams.width = wh;
		imageView.setLayoutParams(layoutParams);
	}
}
